package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.tool.CObjectTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.sys.bean.SysProductDiycatBean;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.List;
import java.util.Map;

public class SysProductDiycatDao extends CObject {
    //private declarations
    private IDbDrv db = null;

    //constructor function to initialize database access object
    public SysProductDiycatDao() {
        db = CloudDbDrvUtils.getDbUtilsDrv();
    }

    //to free resource
    @Override
    public void dispose() {
        if (db != null) {
            db.closeConnection();
        }
        db = null;
        super.dispose();
    }

    //new record to database by map
    public int newSysProductDiycat(Map<String, Object> values) {
        //代理商数据隔离
        values.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.insertRecord("sys_product_diycat", values);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        this.setErrCode(err);
        return ret;
    }

    //to delete table record by id
    public int delSysProductDiycat(int id) {
        String sql = "delete from sys_product_diycat where diyId=?";

        Object[] params = {id};
        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.getAgentId());

        int ret = db.executeSql(sql, params);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        this.setErrCode(err);
        return ret;
    }

    //to delete table record by cdt
    public int delSysProductDiycat(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.deleteRecord("sys_product_diycat", cdt);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        this.setErrCode(err);
        return ret;
    }

    //to modify table record by values and condition
    public int modSysProductDiycat(Map<String, Object> values, Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.modifyRecord("sys_product_diycat", values, cdt);
        String err = db.getErrCode();
        this.setErrCode(err);
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        return ret;
    }

    //to query sys_product_diycat item for detail
    public SysProductDiycatBean querySysProductDiycat(int id) {
        String sql = "select * from sys_product_diycat where diyId=?";

        Object[] params = {id};
        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.getAgentId());

        SysProductDiycatBean ret = db.queryForBean(sql, SysProductDiycatBean.class, params);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(err);
        return ret;
    }

    //to query SysProductDiycat record count
    public int querySysProductDiycat(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.queryRecordCount("sys_product_diycat", cdt);
        this.setErrCode(db.getErrCode());
        return ret;
    }

    //to query SysProductDiycatBean for record list with condition,if page or pagesize less than 0, means without page
    public List<SysProductDiycatBean> querySysProductDiycat(Map<String, Object> cdt, String orderby, int page, int pagesize) {
        //代理商数据隔离
        //cdt.put("agentId", CloudContextUtils.getAgentId());
        cdt.put("agentId", 0);

        List<SysProductDiycatBean> rets = db.queryForBeans("sys_product_diycat", "*", orderby, SysProductDiycatBean.class, cdt, page, pagesize);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        this.setErrCode(err);
        return rets;
    }
}